package com.example.leetdemo.finash;

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution946 {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Deque<Integer> deque = new ArrayDeque<>();
        Deque<Integer> re = new ArrayDeque<>();
        for (int i = 0; i < popped.length; i++) {
            deque.addLast(popped[i]);
        }
        for (int i = 0; i < pushed.length; i++) {
            re.add(pushed[i]);
            while (re.size() > 0 && re.peekLast().equals(deque.peek())) {
                re.pollLast();
                deque.poll();
            }
        }
        if (re.size() > 0) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Solution946 solution946 = new Solution946();
        System.err.println(solution946.validateStackSequences(new int[] {1,2,3,4,5}, new int[] {4,5,3,1,2}));
    }
}
